package com.kwz.common.ui.components.initializers;

import java.awt.Component;
import javax.swing.JTextField;
import javax.swing.text.Document;
import com.kwz.common.Context;
import com.kwz.common.IContext;
import com.kwz.common.designpatterns.ExceptionManager;
import com.kwz.common.designpatterns.factory.BadContextException;
import com.kwz.common.ui.components.IUiComponentContextInitializer;
import com.kwz.common.ui.components.IUiComponentInitializer;

/**
 * <p>
 * 		Initialiseur par defaut pour JTextField
 * </p> 
 *
 * @author mbi
 */
public class UiDefaultJTextFieldInitializer extends UiDefaultComponentInitializer implements IUiComponentInitializer, IrUiInitializers {

    /**
     * 
     */
    public UiDefaultJTextFieldInitializer() {
        super();
        
    }

    /* (non-Javadoc)
     * @see com.kwz.common.ui.components.IUiComponentInitializer#init(java.awt.Component, com.kwz.common.IContext)
     */
    public void init(Component component, IContext ctx) throws BadContextException {
        
        try{
        	JTextField textField = (JTextField) component;
        	
	        //Initialisation du contenu de JTextField
	        if(ctx.containsKey(UI_INIT_TEXT)){
	            textField.setText(ctx.getStringProperty(UI_INIT_TEXT));	            
	        }

	        //Initialisation du nombre de colonnes
	        if(ctx.containsKey(UI_INIT_TEXT_NB_COLUMNS)){
	            textField.setColumns(ctx.getIntProperty(UI_INIT_TEXT_NB_COLUMNS));	            
	        }
	        
	        //Initialisation de l'objet Document du JTextField
	        if(ctx.containsKey(UI_INIT_TEXT_DOCUMENT)){
	            textField.setDocument((Document)ctx.getObjectProperty(UI_INIT_TEXT_DOCUMENT));	            
	        }
	        	        	        
	        //Initialisation de l'alignement horizontal
	        if(ctx.containsKey(UI_INIT_LABEL_ALIGNMENT)){
	        	textField.setHorizontalAlignment(ctx.getIntProperty(UI_INIT_LABEL_ALIGNMENT));
	        }

        }catch(Exception ce){
            ExceptionManager.processExceptionNotThrow(this, ce);
        }

    }

    /* (non-Javadoc)
     * @see com.kwz.common.ui.components.IUiComponentInitializer#getDefaultInitializerContext()
     */
    public IContext getDefaultInitializerContext() {
        return getDefaultInitializerContext(getContextInitializer());
        
    }

    /* (non-Javadoc)
     * @see com.kwz.common.ui.components.IUiComponentInitializer#getDefaultContextInitializer(com.kwz.common.ui.components.IUiComponentContextInitializer)
     */
    public IContext getDefaultInitializerContext(IUiComponentContextInitializer contextInitializer) {
        IContext context = new Context();        
        context.putProperty(UI_INIT_TEXT, contextInitializer.getContextValueFor(UI_JTEXTFIELD, UI_INIT_TEXT));
        context.putProperty(UI_INIT_TEXT_NB_COLUMNS, contextInitializer.getContextValueFor(UI_JTEXTFIELD, UI_INIT_TEXT_NB_COLUMNS));
        context.putProperty(UI_INIT_TEXT_DOCUMENT, contextInitializer.getContextValueFor(UI_JTEXTFIELD, UI_INIT_TEXT_DOCUMENT));
        context.putProperty(UI_INIT_LABEL_ALIGNMENT, contextInitializer.getContextValueFor(UI_JTEXTFIELD, UI_INIT_LABEL_ALIGNMENT));
        return context;
    }

}
